package far;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.sql.Date;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import commans.ChangeCode;
import entity.FirePolicyEntity;

import main.CreatePolicyMain;

//住火要保書底圖無新保單號列印
public class FirePolicyATypeAuto extends CreatePolicyMain 
{
    private List<FirePolicyEntity> Obj;
    private String name;
    private String Unit_Old;
    private String Unit_new;
    private Document document = new Document();
    private Image jpeg = null;

    public FirePolicyATypeAuto(List<FirePolicyEntity> policys)
    {
        setVisible(true);
        this.Obj = policys;
        addWindowListener(new WindowAdapter()
        {
	  public void windowClosing(WindowEvent e)
	  {
	      Map liveThreads = Thread.getAllStackTraces();
	      for(java.util.Iterator i = liveThreads.keySet().iterator(); i.hasNext();)
	      {
		Thread key = (Thread)i.next();
		if(key.getName().equals("CreatePolicy"))
		{
		    key.stop();
		    System.out.println("Thread "+key.getName());
		    document.close();
		    deleteFile(path);
		}
	      }
	  }
        });
    }

    public void CreatePolicy()
    {
        int num1 = 0;
        int num2 = 0;
        int num3 = 0;
        commit = 0;
        try
        {
	  // count total policys
	  for(FirePolicyEntity ply : this.Obj)
	  {
	      count = count+1;
	  }
	  totalBytes = count;
	  jpeg = Image.getInstance(FirePolicyATypeAuto.class.getResource("/A.jpg"));
	  jpeg.setAlignment(Image.UNDERLYING);
	  jpeg.scaleAbsolute(590,840);
	  jpeg.setAbsolutePosition(0,0);
	  bfChinese = BaseFont.createFont("C:\\windows\\fonts\\MINGLIU.TTC,1",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
	  Font FontChinese = new Font(bfChinese,10,Font.NORMAL);
	  Font FontChinese1 = new Font(bfChinese,7,Font.NORMAL);
	  Font FontChinese2 = new Font(bfChinese,8,Font.NORMAL);
	  document.open();

	  for(FirePolicyEntity ply : this.Obj)
	  {

	      Unit_new = ply.getUnitName();
	      // create pdf and group by unit
	      if(Unit_new.equals(Unit_Old))
	      {
		document.add(jpeg);
		commit = commit+1;
		// write pdf
		// 保單號
		document.add(new Paragraph(-2,"                                                                                                "+ply.getNEWPLY(),FontChinese));
		// document.add(new
		// Paragraph("                                                                            "+row[35]+"                                  ",FontChinese));
		// 被保人
		document.add(new Paragraph(21,"                   "+ply.getFASRCNAME()+"                                                                                             "+ply.getAREANO()+ply.getADDR().substring(0,15)+"                     ",FontChinese2));
		document.add(new Paragraph(-12,"                                                                                                                                                                          "+ply.getADDR().substring(15),FontChinese2));
		document.add(new Paragraph(12,"                                               "+ply.getFASRIDNO()+"                   "+ply.getTEL(),FontChinese));
		// 要保人
		document.add(new Paragraph(14,"                   "+ply.getINSDNAME()+"                                                                                             "+ply.getAREANO()+ply.getADDR().substring(0,15)+"                     ",FontChinese2));
		document.add(new Paragraph(-5,"                                                                                                                                                                          "+ply.getADDR().substring(15),FontChinese2));
		document.add(new Paragraph(12,"                                              "+ply.getIDNO()+"                   "+ply.getTEL(),FontChinese));
		// 總保額
		num1 = 0;
		if(ply.getFIREPREM().trim()!=null)
		{
		    num1 = num1+Integer.parseInt(ply.getFIREPREM().trim());
		}
		if(ply.getEPREM().trim()!=null)
		{
		    num1 = num1+Integer.parseInt(ply.getEPREM().trim());
		}
		String money = function.CatZero(num1);
		document.add(new Paragraph(16,"                                  "+function.CatZero(ply.getINSDAMT()),FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                              "+function.CatZero(ply.getFIREPREM()),FontChinese));
		document.add(new Paragraph(18,"                                                                                                      "+money,FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                              "+function.CatZero(ply.getEPREM()),FontChinese));
		// 地震險保額險
		document.add(new Paragraph(16,"                                  "+function.CatZero(ply.getEAMT())+"                                                                                                                    0",FontChinese));
		// 保險期間
		num2 = 1+Integer.parseInt(ply.getEXPDAY().substring(0,3));
		document.add(new Paragraph(18,"                  12                       "+ply.getEXPDAY().substring(0,3)+"         "+ply.getEXPDAY().substring(3,5)+"        "+ply.getEXPDAY().substring(5)+"                                        "+num2+"       "+ply.getEXPDAY().substring(3,5)+"        "+ply.getEXPDAY().substring(5),FontChinese));
		// 標的物地址
		document.add(new Paragraph(30,"                   "+ply.getLOCATION().trim()+ply.getAREA(),FontChinese));
		// 建築物
		for(int i = 0; i<=13; i++)
		{
		    str3[i] = "  ";
		}
		try
		{
		    num1 = Integer.parseInt(ply.getCONSTCLASS());
		}
		catch (NumberFormatException e2)
		{
		    num1 = 0;
		}

		if(num1==1)
		{
		    str4 = "A1";
		}
		else if(num1==2)
		{
		    str4 = "A2";
		}
		else if(num1==3)
		{
		    str4 = "B";
		}
		else
		{
		    str4 = "C";
		}
		// document.add(new Paragraph(" "));
		document.add(new Paragraph(8,"             "+str3[0]+"               "+str3[1]+str3[2],FontChinese1));
		document.add(new Paragraph("             "+str3[3]+"               "+str3[4]+str3[5]+str3[6]+str3[7],FontChinese1));
		document.add(new Paragraph("             "+str3[8]+"               "+str3[9]+str3[10]+str3[11]+"                                                                                                                                                                                       "+ply.getFLOOR()+"                              "+str4,FontChinese1));
		// 建築年份
		document.add(new Paragraph("                                                                        "+ply.getSTRUYEAR()+str3[12]+str3[13],FontChinese1));
		document.add(new Paragraph(0,"                                                                                               "+ply.getCONSTMARK(),FontChinese1));
		document.add(new Paragraph("                                                                                "+CatZero(ply.getAREASIZE()),FontChinese1));
		document.add(new Paragraph(90,"   ",FontChinese));
		// 分項保費建築物
		document.add(new Paragraph(22,"     01                                                                              "+function.CatZero(ply.getINSDAMT())+"          "+ply.getRATE().substring(1)+"    1/1        "+function.CatZero(ply.getFIREPREM()),FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                                                     "+str4,FontChinese));
		// 加保動產
		document.add(new Paragraph(12,"  ",FontChinese));
		document.add(new Paragraph(12,"  ",FontChinese));
		document.add(new Paragraph(12,"          自101年1月1日起，住宅地震基本保險最高賠償責任調整為新台幣150萬元。",FontChinese));
		// 基本地震
		document.add(new Paragraph(20,"     02                                                                              "+function.CatZero(ply.getEAMT())+"          "+"0.9        "+"1/1        "+function.CatZero(ply.getEPREM()),FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                                                     "+str4,FontChinese));
		// 備註
		document.add(new Paragraph(" "));
		document.add(new Paragraph(" "));
		document.add(new Paragraph(" "));
		// 抵押權人 //貸款編號
		document.add(new Paragraph(8,"                                                                                                              "+ply.getBANKKEY(),FontChinese));
		document.add(new Paragraph(14,"                                                                                                     "+ply.getMTG()+"                                        "+ply.getAGENTUNIT(),FontChinese));
		document.add(new Paragraph(16,"                                                                                                                                                         "+ply.getAGENTEMPNO(),FontChinese));
		// 其他
		document.add(new Paragraph(" "));
		document.add(new Paragraph(" "));
		document.add(new Paragraph(68,"            "+ply.getAGENT().trim()+"               "+ply.getMTG().trim()+"                   "+ply.getBROKER()+"                   "+ply.getEMPA()+"                   "+ply.getEMPA1()+"                          "+ply.getSTUNIT()+"                          "+ply.getSEMP1().trim(),FontChinese));
		document.add(new Paragraph(14,"16.9% + 6%",FontChinese));
		document.newPage();
		// save first unit
		Unit_Old = Unit_new;
	      }
	      else
	      {

		document.close();
		path = "C:\\"+Unit_new+"-住火要保書"+new ChangeCode().Gettimer()+".pdf";
		document = new Document(PageSize.A4,72,36,173,-60);
		PdfWriter.getInstance(document,new FileOutputStream(path));
		// font
		document.addTitle("Mingtai");
		document.addAuthor("MINGTAI");
		document.addSubject("MINGTAI");
		document.addKeywords("MINGTAI");
		document.addCreator("MINGTAI");
		document.open();
		document.add(jpeg);
		commit = commit+1;
		// write pdf
		// 保單號
		document.add(new Paragraph(-2,"                                                                                                "+ply.getNEWPLY(),FontChinese));
		// document.add(new
		// Paragraph("                                                                            "+row[35]+"                                  ",FontChinese));
		// 被保人
		document.add(new Paragraph(21,"                   "+ply.getFASRCNAME()+"                                                                                             "+ply.getAREANO()+ply.getADDR().substring(0,15)+"                     ",FontChinese2));
		document.add(new Paragraph(-12,"                                                                                                                                                                          "+ply.getADDR().substring(15),FontChinese2));
		document.add(new Paragraph(12,"                                               "+ply.getFASRIDNO()+"                   "+ply.getTEL(),FontChinese));
		// 要保人
		document.add(new Paragraph(14,"                   "+ply.getINSDNAME()+"                                                                                             "+ply.getAREANO()+ply.getADDR().substring(0,15)+"                     ",FontChinese2));
		document.add(new Paragraph(-5,"                                                                                                                                                                          "+ply.getADDR().substring(15),FontChinese2));
		document.add(new Paragraph(12,"                                              "+ply.getIDNO()+"                   "+ply.getTEL(),FontChinese));
		// 總保額
		num1 = 0;
		if(ply.getFIREPREM().trim()!=null)
		{
		    num1 = num1+Integer.parseInt(ply.getFIREPREM().trim());
		}
		if(ply.getEPREM().trim()!=null)
		{
		    num1 = num1+Integer.parseInt(ply.getEPREM().trim());
		}
		String money = function.CatZero(num1);
		document.add(new Paragraph(16,"                                  "+function.CatZero(ply.getINSDAMT()),FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                              "+function.CatZero(ply.getFIREPREM()),FontChinese));
		document.add(new Paragraph(18,"                                                                                                      "+money,FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                              "+function.CatZero(ply.getEPREM()),FontChinese));
		// 地震險保額險
		document.add(new Paragraph(16,"                                  "+function.CatZero(ply.getEAMT())+"                                                                                                                    0",FontChinese));
		// 保險期間
		num2 = 1+Integer.parseInt(ply.getEXPDAY().substring(0,3));
		document.add(new Paragraph(18,"                  12                       "+ply.getEXPDAY().substring(0,3)+"         "+ply.getEXPDAY().substring(3,5)+"        "+ply.getEXPDAY().substring(5)+"                                        "+num2+"       "+ply.getEXPDAY().substring(3,5)+"        "+ply.getEXPDAY().substring(5),FontChinese));
		// 標的物地址
		document.add(new Paragraph(30,"                   "+ply.getLOCATION().trim()+ply.getAREA(),FontChinese));
		// 建築物
		for(int i = 0; i<=13; i++)
		{
		    str3[i] = "  ";
		}
		try
		{
		    num1 = Integer.parseInt(ply.getCONSTCLASS());
		}
		catch (NumberFormatException e2)
		{
		    num1 = 0;
		}

		if(num1==1)
		{
		    str4 = "A1";
		}
		else if(num1==2)
		{
		    str4 = "A2";
		}
		else if(num1==3)
		{
		    str4 = "B";
		}
		else
		{
		    str4 = "C";
		}
		// document.add(new Paragraph(" "));
		document.add(new Paragraph(8,"             "+str3[0]+"               "+str3[1]+str3[2],FontChinese1));
		document.add(new Paragraph("             "+str3[3]+"               "+str3[4]+str3[5]+str3[6]+str3[7],FontChinese1));
		document.add(new Paragraph("             "+str3[8]+"               "+str3[9]+str3[10]+str3[11]+"                                                                                                                                                                                       "+ply.getFLOOR()+"                              "+str4,FontChinese1));
		// 建築年份
		document.add(new Paragraph("                                                                        "+ply.getSTRUYEAR()+str3[12]+str3[13],FontChinese1));
		document.add(new Paragraph(0,"                                                                                               "+ply.getCONSTMARK(),FontChinese1));
		document.add(new Paragraph("                                                                                "+CatZero(ply.getAREASIZE()),FontChinese1));
		document.add(new Paragraph(90,"   ",FontChinese));
		// 分項保費建築物
		document.add(new Paragraph(22,"     01                                                                              "+function.CatZero(ply.getINSDAMT())+"          "+ply.getRATE().substring(1)+"    1/1        "+function.CatZero(ply.getFIREPREM()),FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                                                     "+str4,FontChinese));
		// 加保動產
		document.add(new Paragraph(12,"  ",FontChinese));
		document.add(new Paragraph(12,"  ",FontChinese));
		document.add(new Paragraph(12,"          自101年1月1日起，住宅地震基本保險最高賠償責任調整為新台幣150萬元。",FontChinese));
		// 基本地震
		document.add(new Paragraph(20,"     02                                                                              "+function.CatZero(ply.getEAMT())+"          "+"0.9        "+"1/1        "+function.CatZero(ply.getEPREM()),FontChinese));
		document.add(new Paragraph(0,"                                                                                                                                                                                     "+str4,FontChinese));
		// 備註
		document.add(new Paragraph(" "));
		document.add(new Paragraph(" "));
		document.add(new Paragraph(" "));
		// 抵押權人 //貸款編號
		document.add(new Paragraph(8,"                                                                                                              "+ply.getBANKKEY(),FontChinese));
		document.add(new Paragraph(14,"                                                                                                     "+ply.getMTG()+"                                        "+ply.getAGENTUNIT(),FontChinese));
		document.add(new Paragraph(16,"                                                                                                                                                         "+ply.getAGENTEMPNO(),FontChinese));
		// 其他
		document.add(new Paragraph(" "));
		document.add(new Paragraph(" "));
		document.add(new Paragraph(68,"            "+ply.getAGENT().trim()+"               "+ply.getMTG().trim()+"                   "+ply.getBROKER()+"                   "+ply.getEMPA()+"                   "+ply.getEMPA1()+"                          "+ply.getSTUNIT()+"                          "+ply.getSEMP1().trim(),FontChinese));
		document.add(new Paragraph(14,"16.9% + 6%",FontChinese));
		document.newPage();
		// save first unit
		Unit_Old = Unit_new;
	      }
	      // now work policy and update progressbar
	      byteRead = byteRead+1;
	      currentValue = (int) ( byteRead*100/totalBytes );
	      progressBar.setValue(currentValue);
	  }
	  JOptionPane.showMessageDialog(null,"產生完成");
	  dispose();
	  document.close();
        }
        catch (Exception e1)
        {
	  document.close();
	  deleteFile(path);
	  ErrorMessage(e1);
        }
        finally
        {
	  document.close();
	  System.gc();
	  dispose();
        }
    }

    public void run()
    {
        CreatePolicy();
    }

}
